// $(function () {
//     findAll()
// })



function edit(id) {
    // console.log(id,"---------")
    sessionStorage.setItem("userId", id);
    xadmin.open('编辑', 'member-edit.html', 600, 400)
}

function update(id) {
    console.log(id, "---------")
    sessionStorage.setItem("id", id);
    xadmin.open('编辑', 'member-password.html', 600, 400)
}

function del(id) {
    $.ajax({
        url: '/back/user/delete',
        data: {id: id},
        type: 'post',
        dataType: 'json',
        success: function (data) {
            console.log(data.msg, "aoaoaoaoao");
            if (data.msg == 'success') {
                layer.alert("删除成功", {
                        icon: 6
                    },
                    function () {
                        // 可以对父窗口进行刷新
                        xadmin.father_reload();
                    });
            } else {
                layer.msg("删除失败");
            }
        }
    });
}

/*用户-停用*/
function member_stop(obj, id, enable) {
    layer.confirm('确认要' + (enable == 1 ? "停用" : "启用") + '吗？', function (index) {

        $.ajax({
            url: '/back/user/edit',
            data: {id: id, enable: enable == 1 ? 0 : 1},
            type: 'post',
            dataType: 'json',
            success: function (data) {
                if (data.msg == 'success') {
                    if ($(obj).attr('title') == '启用') {
                        //发异步把用户状态进行更改
                        $(obj).attr('title', '停用')
                        $(obj).find('i').html('&#xe62f;');
                        $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                        layer.msg('已停用!', {icon: 5, time: 1000});
                    } else {
                        $(obj).attr('title', '启用')
                        $(obj).find('i').html('&#xe601;');
                        $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                        layer.msg('已启用!', {icon: 6, time: 1000});
                    }
                    // 可以对父窗口进行刷新
                    xadmin.father_reload();
                } else {
                    layer.msg('修改失败!', {icon: 5, time: 1000});
                }
            }
        });
    });
}

layui.use(['form', 'layer', 'laydate', 'laypage'],
    function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate
            , laypage = layui.laypage;
        //日期
        laydate.render({
            elem: '#start',
            trigger: 'click'
        });
        laydate.render({
            elem: '#end',
            trigger: 'click'
        });
        laydate.render({
            elem: '#birthday',
            trigger: 'click'
        });
        //监听提交
        form.on('submit(find)',
            function (data) {
                // console.log(data);
                data = data.field;
                let arr_box = [];
                $('input[type=checkbox][name=hobby]:checked').each(function () {
                    arr_box.push($(this).attr("title"));
                });
                data.hobby = arr_box.toLocaleString();
                //发异步，把数据提交给php
                // console.log(data,"应该有username1")
                page(data);
                return false;
            });
        //完整功能
        page({});
    });

function page(data) {
    layui.use(['laypage'], function () {
        var laypage = layui.laypage;
        // console.log(data,"应该有username2")
        //完整功能
        laypage.render({
            elem: 'page'
            , count: getCount(data)
            , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            , jump: function (obj) {
                // console.log(obj)
                findAll(obj.curr, obj.limit, data);
            }
        });
    });
}

function getCount(data) {
    // console.log(data,"应该有username3")
    let count = null;
    $.ajax({
        url: '/back/user/getCount',
        data: data,
        type: 'get',
        async: false,
        dataType: 'json',
        success: function (res) {
            count = res.count;
        }
    });
    return count;
}

function findAll(curr, limit, data) {
    // let data = {username: 'a'};
    let page = {pageNo: curr, pageSize: limit};
    // data.pageNo = curr;
    // data.pageSize = limit;
    // console.log(data,"99999")
    $.ajax({
        url: '/back/user/findAll',
        data: Object.assign(data, page),
        type: 'get',
        dataType: 'json',
        success: function (res) {
            // console.log(res);
            if (res.code==-1){
                window.open("/html/login.html", "_top");
                return;
            }
            let data = res.data;
            let html = '';
            for (let i = 0; i < data.length; i++) {
                html += ' <tr>\n' +
                    '                                    <td>' + data[i].id + '</td>\n' +
                    '                                    <td>' + data[i].username + '</td>\n' +
                    '                                    <td>' + data[i].sex + '</td>\n' +
                    '                                    <td>' + data[i].phone + '</td>\n' +
                    '                                    <td>' + (data[i].address == undefined ? '' : data[i].address) + '</td>\n' +
                    '                                    <td>' + (data[i].hobby == undefined ? '' : data[i].hobby) + '</td>\n' +
                    '                                    <td>' + data[i].birthday + '</td>\n' +
                    '                                    <td class="td-status">\n' +
                    '                                      <span class="layui-btn layui-btn-normal layui-btn-mini ' + (data[i].enable == 1 ? '' : 'layui-btn-disabled') + '">' + (data[i].enable == 1 ? '已启用' : '已停用') + '</span></td>\n' +
                    '                                    <td class="td-manage">\n' +
                    '                                      <a onclick="member_stop(this,' + data[i].id + ',' + data[i].enable + ')" href="javascript:;"  title="' + (data[i].enable == 1 ? '启用' : '停用') + '">\n' +
                    '                                        <i class="layui-icon">' + (data[i].enable == 1 ? '&#xe601;' : '&#xe62f;') + '</i>\n' +
                    '                                      </a>\n' +
                    '                                      <a title="编辑"  onclick="edit(' + data[i].id + ')" href="javascript:;">\n' +
                    '                                        <i class="layui-icon">&#xe642;</i>\n' +
                    '                                      </a>\n' +
                    '                                      <a onclick="update(' + data[i].id + ')" title="修改密码" href="javascript:;">\n' +
                    '                                        <i class="layui-icon">&#xe631;</i>\n' +
                    '                                      </a>\n' +
                    '                                      <a title="删除" onclick="del(' + data[i].id + ')" href="javascript:;">\n' +
                    '                                        <i class="layui-icon">&#xe640;</i>\n' +
                    '                                      </a>\n' +
                    '                                      <a title="蛋糕"  href="javascript:;">\n' +
                    '                                       <i class="iconfont" style="color: red">&#xe64d;</i>\n' +
                    '                                      </a>\n' +
                    '                                    </td>\n' +
                    '</tr>';
            }
            $("#tbody").html(html);
        }
    });

}



